﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestUserControl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            LoginFormUC.LoginForm loginForm = new LoginFormUC.LoginForm("admin");
            loginForm.ShowDialog();
            string alertMessage = loginForm.IsAdmin ? "Admin" : "Not Admin";
            MessageBox.Show(alertMessage);
        }

        private void btnChangePassword_Click(object sender, EventArgs e)
        {
            ChangePasswordUC.ChangePasswordForm changePassword = new ChangePasswordUC.ChangePasswordForm("admin");
            changePassword.ShowDialog();
            MessageBox.Show(changePassword.NewPassword);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            imageView1.setImageView(cFviImageView1, true);
        }

        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "bmp files (*.bmp)|*.bmp";
            openFileDialog.Title = "Load Image";
            FVIL.Data.CFviImage src = new FVIL.Data.CFviImage();
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                if (!String.IsNullOrEmpty(openFileDialog.FileName))
                {
                    try
                    {
                        src.Load(openFileDialog.FileName);
                        if (src.Depth != 8 || src.Channel != 1 || src.Bpp != 8)
                            throw new System.Exception();
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Invalid image");
                        return;
                    }
                    cFviImageView1.Image = src;
                    cFviImageView1.FitImageSize();
                    imageView1.updateZoomScale(cFviImageView1.Display.Magnification);
                    cFviImageView1.FitImageSize();
                    cFviImageView1.Refresh();
                }
            }
        }

        private void btnSetRegion_Click(object sender, EventArgs e)
        {
            SetRegionUC.SetRegionForm setRegionForm = new SetRegionUC.SetRegionForm();
            setRegionForm.setImageView(cFviImageView1);
            setRegionForm.Show(this);
        }

        private void btnPatternRegist_Click(object sender, EventArgs e)
        {
            PatternRegistUC.PatternRegist pp = new PatternRegistUC.PatternRegist();
            pp.setImageView(cFviImageView1);
            pp.Show(this);
        }

        private void camera1_Load(object sender, EventArgs e)
        {
            camera1.setImageView(cFviImageView1, "CameraIni.xml", 0);
            camera1.eventPlay += new EventHandler(OnPlayCamera);
            camera1.eventPause += new EventHandler(OnPauseCamera);
        }

        private void OnPlayCamera(object sender, EventArgs e)
        {
            btnCameraSetting.Enabled = false;
        }

        private void OnPauseCamera(object sender, EventArgs e)
        {
            btnCameraSetting.Enabled = true;
        }

        private void btnCameraSetting_Click(object sender, EventArgs e)
        {
            camera1.releaseCamera();
            CameraSettingUC.CameraSettingForm cameraForm = new CameraSettingUC.CameraSettingForm("CameraIni.xml");
            cameraForm.ShowDialog();
            camera1.initCamera();
        }

        private void btnStartTest_Click(object sender, EventArgs e)
        {
            Random rng = new Random();
            bool randomBool = rng.Next(0, 2) > 0;
            double time = (double) rng.Next(1, 100) / 100;
            timeExecution1.updateTimeExecution(randomBool, time);
            counter1.updateCounter(randomBool);
            history1.updateHistory(randomBool, time, "E:\\Image.bmp");
        }
    }
}
